package com.help10000.rms.ui.activitys.mine.feedback

import android.app.Activity
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.fastjson.JSONObject
import com.amir.common.beans.FeedBackEntity
import com.amir.common.utils.DisplayUtil
import com.amir.common.utils.ToastUtil
import com.amir.common.weight.DuplicateClickProxy
import com.help10000.rms.R
import com.help10000.rms.common.GridSpacingItemDecoration
import com.help10000.rms.common.WeChatPresenter
import com.help10000.rms.mvp.presenter.FeedbackListPresenter
import com.help10000.rms.mvp.view.FeedbackView
import com.help10000.rms.ui.adapter.OrderReportPhotoItemAdapter
import com.help10000.rms.ui.base.BaseActivity
import com.ypx.imagepicker.ImagePicker
import com.ypx.imagepicker.bean.MimeType
import kotlinx.android.synthetic.main.activity_add_address.*
import kotlinx.android.synthetic.main.activity_add_feedback.*
import kotlinx.android.synthetic.main.activity_add_feedback.btnSave

class AddFeedbackActivity : BaseActivity<FeedbackListPresenter, FeedbackView>(), FeedbackView {
    override fun getlayoutId(): Int {
        return R.layout.activity_add_feedback
    }

    var fileList = mutableListOf<String>()
    val list = mutableListOf<String>()
    var mAdapter: OrderReportPhotoItemAdapter? = null
    override fun initView() {

    }

    override fun initData(savedInstanceState: Bundle?) {
        mAdapter = OrderReportPhotoItemAdapter(
            this,
            list,
            5,
            R.layout.item_pic_layout
        )
        attachRecyclerView.apply {
            layoutManager = GridLayoutManager(context, 3)
            adapter = mAdapter
            itemAnimator = null

        }
        var padding = DisplayUtil.dip2px(this, 10f)
        attachRecyclerView.addItemDecoration(GridSpacingItemDecoration(padding, true))
        mAdapter?.setOnItemClickListener { pos ->
            if (list.size >= 5) {
                return@setOnItemClickListener
            }
            if (pos == list.size) {
                ImagePicker.withMulti(WeChatPresenter())//设置presenter
                    .setMaxCount(5-list.size)//设置选择数量
                    .showCamera(true)//设置显示拍照
                    .setPreview(true)
                    .setColumnCount(4)//设置列数
                    .setPreview(true)
                    .mimeTypes(MimeType.ofImage())//设置需要加载的文件类型
                    .filterMimeTypes(MimeType.GIF)//设置需要过滤掉的文件类型
                    // .assignGapState(false)//强制留白模式
                    // .setFirstImageItem(null)//设置上一次选中的图片
                    //.setFirstImageItemSize(1, 1)//设置上一次选中的图片地址
                    .setVideoSinglePick(false)//设置视频单选
                    .setMaxVideoDuration(1200000L)//设置可选区的最大视频时长
                    .setMinVideoDuration(60000L)//设置视频可选取的最小时长
                    .pick(mContext as Activity?) { items ->
                        if (items.size > 0) {
                            for (item in items) {
                                list.add("file://" + item.path)
                            }
                            mAdapter?.notifyDataSetChanged()
                        }
                    }
            }
        }
    }

    var isFileUpload = false
    override fun dismissLoading() {
        if (!isFileUpload) {
            super.dismissLoading()
        }
    }

    override fun onError(mess: String?) {
        super.onError(mess)
        isFileUpload = false
        btnSave.isEnabled = true
    }

    override fun initListener() {
        btnSave.setOnClickListener(DuplicateClickProxy {
            var comment = Comments.text.toString()
            if (comment.isEmpty()) {
                ToastUtil.show("请输入反馈内容")
                return@DuplicateClickProxy
            }
            btnSave.isEnabled = false
            if (list.size > 0) {
                isFileUpload = true
                mPresenter.upFile(list)
            } else {
                fileUploadSuccess(mutableListOf())
            }
        })
    }

    override fun createPresenter(): FeedbackListPresenter {
        return FeedbackListPresenter(this)
    }

    override fun onSuccess() {
        ToastUtil.show("反馈成功")
        finish()
    }

    override fun deleteSuccess(pos: Int) {

    }

    override fun fileUploadSuccess(data: MutableList<String>) {
        fileList = data
        isFileUpload = false
        var comment = Comments.text.toString()
        mPresenter.addFeed(comment, JSONObject.toJSONString(fileList))
    }

    override fun onDataSuccess(data: FeedBackEntity) {

    }
}